package com.android.clinic.app.picture;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.picture.bean.FunctionConfig;
import com.android.clinic.app.picture.bean.LocalMedia;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/2/6.
 */
public class PictureBaseActivity extends BaseActivity {

    protected Context mContext;
    protected int type = 0;
    protected int maxSelectNum = 0;
    protected int spanCount = 4;
    protected boolean showCamera = false;
    protected boolean enablePreview = false;
    protected int selectMode = FunctionConfig.MODE_MULTIPLE;
    protected int backgroundColor = 0;
    protected boolean isCompress;
    protected boolean is_checked_num;

    protected List<LocalMedia> selectMedias = new ArrayList<>();
    protected FunctionConfig config = new FunctionConfig();

    @Override
    protected int getLayoutResId() {
        return getLayoutResId();
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        mContext = this;
        ImagePipelineConfig pipelineConfig = ImagePipelineConfig.newBuilder(this)
                .setDownsampleEnabled(true)
                .build();
        Fresco.initialize(this, pipelineConfig);

        config = (FunctionConfig) getIntent().getSerializableExtra(FunctionConfig.EXTRA_THIS_CONFIG);
        type = config.getType();
        showCamera = config.isShowCamera();
        enablePreview = config.isEnablePreview();
        selectMode = config.getSelectMode();
        maxSelectNum = config.getMaxSelectNum();
        backgroundColor = config.getThemeStyle();
        isCompress = config.isCompress();
        spanCount = config.getImageSpanCount();
        is_checked_num = config.isCheckNumMode();
        selectMedias = config.getSelectMedia();
    }

    /**
     * 针对6.0动态请求权限问题
     * 判断是否允许此权限
     *
     * @param permissions
     * @return
     */
    protected boolean hasPermission(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 动态请求权限
     *
     * @param code
     * @param permissions
     */
    protected void requestPermission(int code, String... permissions) {
        ActivityCompat.requestPermissions(this, permissions, code);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case FunctionConfig.READ_EXTERNAL_STORAGE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readLocalMedia();
                } else {
                    showToast("读取内存卡权限已被拒绝");
                }
                break;
            case FunctionConfig.CAMERA:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startCamera();
                } else {
                    showToast("拍照权限已被拒绝");
                }
                break;
        }
    }

    /**
     * 启动相机
     */
    protected void startCamera() {

    }

    /**
     * 读取相册信息
     */
    protected void readLocalMedia() {

    }

    /**
     * 广播发送者
     *
     * @param action
     */
    protected void sendBroadcast(String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        sendBroadcast(intent);
    }


    /**
     * 注册发送者
     *
     * @param action
     */
    protected void registerReceiver(BroadcastReceiver receiver, String... action) {
        IntentFilter intentFilter = new IntentFilter();
        for (int i = 0; i < action.length; i++) {
            intentFilter.addAction(action[i]);
        }
        registerReceiver(receiver, intentFilter);
    }
}
